package com.cecilleo.core.base.util.notch;

import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @description: 华为刘海屏适配工具类
 * @date: 2019/2/25 10:16
 */
public class HwNotchUtils {

  /*刘海屏全屏显示FLAG*/
  public static final int FLAG_NOTCH_SUPPORT = 0x00010000;
  public static final String DISPLAY_NOTCH_STATUS = "display_notch_status";

  /**
   * 判断是否有刘海屏
   *
   * @return true：有刘海屏；false：没有刘海屏
   */
  public static boolean hasNotch(Context context) {
    boolean ret = false;
    try {
      ClassLoader cl = context.getClassLoader();
      Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
      Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
      ret = (boolean) get.invoke(HwNotchSizeUtil);
    } catch (ClassNotFoundException e) {
      Log.e("test", "hasNotchInScreen ClassNotFoundException");
    } catch (NoSuchMethodException e) {
      Log.e("test", "hasNotchInScreen NoSuchMethodException");
    } catch (Exception e) {
      Log.e("test", "hasNotchInScreen Exception");
    } finally {
      return ret;
    }
  }

  /**
   * 获取刘海尺寸
   *
   * @return int[0]值为刘海宽度 int[1]值为刘海高度
   */
  public static int[] getNotchSize(Context context) {
    int[] ret = new int[] { 0, 0 };
    try {
      ClassLoader cl = context.getClassLoader();
      Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
      Method get = HwNotchSizeUtil.getMethod("getNotchSize");
      ret = (int[]) get.invoke(HwNotchSizeUtil);
    } catch (ClassNotFoundException e) {
      Log.e("test", "getNotchSize ClassNotFoundException");
    } catch (NoSuchMethodException e) {
      Log.e("test", "getNotchSize NoSuchMethodException");
    } catch (Exception e) {
      Log.e("test", "getNotchSize Exception");
    } finally {
      return ret;
    }
  }

  /**
   * 设置应用窗口在华为刘海屏手机使用刘海区
   * <p>
   * 通过添加窗口FLAG的方式设置页面使用刘海区显示
   *
   * @param window 应用页面window对象
   */
  public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {
    if (window == null) {
      return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
      Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");
      Constructor con = layoutParamsExCls.getConstructor(WindowManager.LayoutParams.class);
      Object layoutParamsExObj = con.newInstance(layoutParams);
      Method method = layoutParamsExCls.getMethod("addHwFlags", int.class);
      method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException
        | InvocationTargetException e) {
      Log.e("test", "hw add notch screen flag api error");
    } catch (Exception e) {
      Log.e("test", "other Exception");
    }
  }

  /**
   * 恢复应用不使用刘海区显示
   * <p>
   * 通过去除窗口FLAG的方式设置页面不使用刘海区显示
   *
   * @param window 应用页面window对象
   */
  public static void setNotFullScreenWindowLayoutInDisplayCutout(Window window) {
    if (window == null) {
      return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
      Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");
      Constructor con = layoutParamsExCls.getConstructor(WindowManager.LayoutParams.class);
      Object layoutParamsExObj = con.newInstance(layoutParams);
      Method method = layoutParamsExCls.getMethod("clearHwFlags", int.class);
      method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException
        | InvocationTargetException e) {
      Log.e("Leo", "hw clear notch screen flag api error");
    } catch (Exception e) {
      Log.e("Leo", "other Exception");
    }
  }

  /**
   * 获取默认和隐藏刘海区开关值
   *
   * @return 0表示“默认”，1表示“隐藏显示区域”
   */
  public static int getIsNotchSwitchOpen(Context context) {
    return Settings.Secure.getInt(context.getContentResolver(), DISPLAY_NOTCH_STATUS, 0);
  }
}